60c6c9ff714e1fbf0b57119e22cea90560a10ea7,languagetool-core/src/main/java/org/languagetool/rules/AbstractWordCoherencyRule.java,AbstractWordCoherencyRule,match,#AnalyzedSentence#,59

Before Change


        RuleMatch otherMatch = shouldNotAppearWord.get(token);
        String otherSpelling = otherMatch.getMessage();
        String msg = getMessage(token, otherSpelling);
        RuleMatch ruleMatch = new RuleMatch(this, tmpToken.getStartPos(), tmpToken.getEndPos(), msg);
        ruleMatch.setSuggestedReplacement(otherSpelling);
        ruleMatches.add(ruleMatch);
      } else if (getWordMap().containsKey(token)) {
        String shouldNotAppear = getWordMap().get(token);
        RuleMatch potentialRuleMatch = new RuleMatch(this, tmpToken.getStartPos(), tmpToken.getEndPos(), token);
        shouldNotAppearWord.put(shouldNotAppear, potentialRuleMatch);
      }
    }

After Change


    List<RuleMatch> ruleMatches = new ArrayList<>();
    Map<String, RuleMatch> shouldNotAppearWord = new HashMap<>();  // e.g. aufwändig -> RuleMatch of aufwendig
    int pos = 0;
    for (AnalyzedSentence sentence : sentences) {
      AnalyzedTokenReadings[] tokens = sentence.getTokensWithoutWhitespace();
      for (AnalyzedTokenReadings tmpToken : tokens) {
        String token = tmpToken.getToken();
        List<AnalyzedToken> readings = tmpToken.getReadings();
        // TODO: in theory we need to care about the other readings, too (affects e.g. German "Schenke" as a noun):
        if (readings.size() > 0) {
          String baseform = readings.get(0).getLemma();
          if (baseform != null) {
            token = baseform;
          }
        }
        if (shouldNotAppearWord.containsKey(token)) {
          RuleMatch otherMatch = shouldNotAppearWord.get(token);
          String otherSpelling = otherMatch.getMessage();
          String msg = getMessage(token, otherSpelling);
          RuleMatch ruleMatch = new RuleMatch(this, pos+tmpToken.getStartPos(), pos+tmpToken.getEndPos(), msg);
          ruleMatch.setSuggestedReplacement(otherSpelling);
          ruleMatches.add(ruleMatch);
        } else if (getWordMap().containsKey(token)) {
          String shouldNotAppear = getWordMap().get(token);
          RuleMatch potentialRuleMatch = new RuleMatch(this, pos+tmpToken.getStartPos(), pos+tmpToken.getEndPos(), token);
          shouldNotAppearWord.put(shouldNotAppear, potentialRuleMatch);
        }
      }
      pos += sentence.getText().length();
    }
    return toRuleMatchArray(ruleMatches);
  }